<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once ("includes/connect.php");
require_once ("includes/function.php");

$bank = $_REQUEST["bank"];
$branch_id = $_REQUEST["branch"];
$bank_type = $_REQUEST["bank_type"];
$a = $_REQUEST["a"];
$c = $_REQUEST["c"];
$val = $_REQUEST["val"];
$payment_method = $_REQUEST["payment_method"];
$i_date = $_REQUEST["i_date"];
$tblname = $_REQUEST["tblname"];

//echo $val[1];
$val1 = explode(",", $val);

//print_r ($val1);

switch ($tblname) {
    case "tbl_pet":
        $fields_pet = "pet_id";
        break;
    case "tbl_petout":
        $fields_pet = "petout_id";
        break;
    default:
        break;
}

if ($payment_method == "เงินสด") {
    if (is_array($val)) {
        foreach ($val as $pet_id) {
            $sql = "INSERT INTO tbl_pay (payment, pet_id) "
                    . " VALUES ('$payment_method', '$pet_id') ";
            echo $sql . "<br>";
            mysql_query($sql) or die(mysql_error());
        }

        $sql = "UPDATE $tblname SET "
                . " status = 'จัดทำ$payment_method' "
                . ", payment_method = '$payment_method' "
                . " WHERE $fields_pet IN ('" . implode("','", $val) . "')";
        echo $sql . "<br>";
        mysql_query($sql) or die(mysql_error());
    } else {

        $sql = "INSERT INTO tbl_pay (payment, pet_id) "
                . " VALUES ('$payment_method', '$val') ";
        echo $sql . "<br>";
        mysql_query($sql) or die(mysql_error());


        $sql = "UPDATE $tblname SET "
                . " status = 'จัดทำ$payment_method' "
                . ", payment_method = '$payment_method' "
                . " WHERE $fields_pet IN ('$val')";
        echo $sql . "<br>";
        mysql_query($sql) or die(mysql_error());
    }
} else {
    if (is_array($val1) && count($val1) > 1) {
        $i = 0;
        foreach ($val1 as $pet_id) {
            $sql = "INSERT INTO tbl_pay (payment, bank_id, bank_branch_id, bank_type, amount, pet_id, cheque_no) "
                    . " VALUES ('$payment_method', '$bank[$i]', '$branch_id[$i]', '$bank_type[$i]', '$a[$i]', '$pet_id', '$c[$i]' ) ";
            echo $sql . "<br>";
            mysql_query($sql) or die(mysql_error());
        }

        $sql = "UPDATE $tblname SET "
                . " status = 'จัดทำ$payment_method' "
                . ", payment_method = '$payment_method' "
                . ", cheque_prt_date = '$i_date' "
                . " WHERE $fields_pet IN ('" . implode("','", $val1) . "')";

        echo $sql . "<br>";
        mysql_query($sql) or die(mysql_error());
    } else {
        if (is_array($a)) {
            $i = 0;
            foreach ($a as $value) {
                if (is_numeric($value)) {
                    //echo $bank[$i];

                    $sql = "INSERT INTO tbl_pay (payment, bank_id, bank_branch_id, bank_type, amount, pet_id, cheque_no) "
                            . " VALUES ('$payment_method', '$bank[$i]', '$branch_id[$i]', '$bank_type[$i]', '$a[$i]', '$val', '$c[$i]' ) ";

                    echo $sql . "<br>";
                    mysql_query($sql) or die(mysql_error());
                }
                $i++;
            }

            $sql = "UPDATE $tblname SET "
                    . " status = 'จัดทำ$payment_method' "
                    . ", payment_method = '$payment_method' "
                    . ", cheque_prt_date = '$i_date' "
                    . " WHERE $fields_pet IN ('$val')";

            echo $sql . "<br>";
            mysql_query($sql) or die(mysql_error());
            //echo "Y". count($bank);
        }
    }
}
?>

